package run.halo.app.core.endpoint.theme;

import java.math.BigDecimal;
import java.time.Instant;
import java.util.List;
import lombok.Builder;
import lombok.Value;
import run.halo.app.core.extension.content.Product;
import run.halo.app.extension.MetadataUtil;

/**
 * Value object for Product.
 *
 * @author halo-product-management
 * @since 2.0.0
 */
@Value
@Builder
public class ProductVo {

    String name;

    String displayName;

    String productTypeRef;

    String description;

    List<String> images;

    BigDecimal price;

    BigDecimal samplePrice;

    Integer minOrderQuantity;

    Integer priority;

    Boolean published;

    String permalink;

    String productTypeName;

    Instant creationTimestamp;

    Instant updateTimestamp;

    public static ProductVo from(Product product) {
        return ProductVo.builder()
            .name(product.getMetadata().getName())
            .displayName(product.getSpec().getDisplayName())
            .productTypeRef(product.getSpec().getProductTypeRef())
            .description(product.getSpec().getDescription())
            .images(product.getSpec().getImages())
            .price(product.getSpec().getPrice())
            .samplePrice(product.getSpec().getSamplePrice())
            .minOrderQuantity(product.getSpec().getMinOrderQuantity())
            .priority(product.getSpec().getPriority())
            .published(product.getSpec().getPublished())
            .permalink(product.getStatusOrDefault().getPermalink())
            .productTypeName(product.getStatusOrDefault().getProductTypeName())
            .creationTimestamp(product.getMetadata().getCreationTimestamp())
            .updateTimestamp(product.getMetadata().getLabels() != null 
                ? product.getMetadata().getCreationTimestamp() 
                : product.getMetadata().getCreationTimestamp())
            .build();
    }
}